package com.ziven.common.widget.test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.ziven.common.widget.WidgetUtils;
import com.ziven.common.widget.alphabet.AlphabetIndex;
import com.ziven.common.widget.app.EasySpate;
import com.ziven.common.widget.app.EasyUtil;
import com.ziven.common.widget.setting.PreferenceFragment;
import com.ziven.common.widget.setting.PreferenceItemsFactory;
import com.ziven.common.widget.title.SearchEffect;
import com.ziven.common.widget.title.SearchView;
import com.ziven.common.widget.value.Title;
import com.ziven.common.widget.window.FragmentContainerActivity;
import com.ziven.easygo.EasyGos;
import com.ziven.easygo.annotation.EasyGoMethod;
import com.ziven.easygo.util.Carry;
import com.ziven.easygo.util.LogHelper;
import com.ziven.easygo.util.Nulls;

/**
 * @author Ziven
 */
public class MainActivity extends AppCompatActivity implements SearchEffect {

    private SearchView searchView;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EasyGos.getEasyGo().register(this);
        EasySpate.addObserver(this)
                .addCreateSpate(() -> LogHelper.log("EasySpate:MainActivity onCreate"))
                .addStartSpate(() -> LogHelper.log("EasySpate:MainActivity onStart"))
                .addResumeSpate(() -> LogHelper.log("EasySpate:MainActivity onResume"))
                .addPauseSpate(() -> LogHelper.log("EasySpate:MainActivity onPause"))
                .addStopSpate(() -> LogHelper.log("EasySpate:MainActivity onStop"))
                .addDestroySpate(() -> LogHelper.log("EasySpate:MainActivity onDestroy"));

        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text_view);
        searchView = findViewById(R.id.search_view);
        searchView.setListener((Carry<String>) data -> {
            if(data.isEmpty()) {
                FragmentContainerActivity
                        .startPreferenceFragmentActivity(Nulls.of(Title.title("设置")), "settings");
            } else if(TextUtils.equals(TestEasyApp.TEST, data)) {
                EasyUtil.easyGo().easyGo(TestEasyApp.TEST);
            } else {
                AlphabetIndex.get().toIndexAlphabet(
                        searchView.getText(),
                        text -> textView.setText(text)
                );
            }
        });

        initEffect();
        WidgetUtils.getSp().edit().putBoolean(PreferenceItemsFactoryImp.TEST_RED_POINT, true).apply();
    }

    @Override
    protected void onDestroy() {
        EasyGos.getEasyGo().unregister(this);
        super.onDestroy();
        EasySpate.removeObserver(this);
    }

    @EasyGoMethod(path = PreferenceFragment.PATH)
    public PreferenceItemsFactory getPreferenceItemsFactory(String factoryId) {
        return new PreferenceItemsFactoryImp();
    }

    @NonNull
    @Override
    public SearchView obtainEffectView() {
        return searchView;
    }
}